#!/bin/bash
#@Author Shahriar Shovon
#@Github https://github.com/shovon8/sysmon
printf "%-10s%-15s%-15s%s\n" "PID" "OWNER" "MEMORY" "COMMAND"

function sysmon_main() {
    RAWIN=$(ps -o pid,user,%mem,command ax | grep -v PID | awk '/[0-9]*/{print $1 ":" $2 ":" $4}')

    for i in $RAWIN
    do
        PID=$(echo $i | cut -d: -f1)
        OWNER=$(echo $i | cut -d: -f2)
        COMMAND=$(echo $i | cut -d: -f3)
        MEMORY=$(pmap $PID | tail -n 1 | awk '/[0-9]K/{print $2}')
        [[ ${MEMORY} == '0K' || -z ${MEMORY} ]] && continue
        
        printf "%-10s%-15s%-15s%s\n" "$PID" "$OWNER" "$((${MEMORY//K/}/1024))Mb" "$COMMAND"
    done
}

sysmon_main | sort -bnr -k3

##显示进程使用内存
ps -eo size,pid,user,command --sort -size | \
    awk '{ hr=$1/1024 ; printf("%13.2f Mb ",hr) } { for ( x=4 ; x<=NF ; x++ ) { printf("%s ",$x) } print "" }' |\
    cut -d "" -f2 | cut -d "-" -f1